package com.example.administrator.damaiwang.fragmentpack;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.administrator.damaiwang.R;

import java.util.ArrayList;
import java.util.List;


public class YanChuFragment extends Fragment {


    private ViewPager mVp;
    private String[] titles;
    private  List<Fragment> fragments;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_yan_chu, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mVp = (ViewPager) view.findViewById(R.id.vp_yanchang);
        TabLayout mTabs = (TabLayout) view.findViewById(R.id.tabs_yanchu);
        Toolbar mTool = (Toolbar) view.findViewById(R.id.tool_bar);
        titles = getTitles();
        fragments = new ArrayList<>();
        for (int i = 0; i < titles.length; i++) {
            AllContentFragment fragment = AllContentFragment.createFragment(i);
            fragments.add(fragment);
        }
        //设置导航图标,也就是Home菜单的图标;
        mTool.setNavigationIcon(R.mipmap.beijing2);
        mTool.inflateMenu(R.menu.main);
        MyAdapter adapter = new MyAdapter(getFragmentManager());

        mVp.setAdapter(adapter);
        mTabs.setTabMode(TabLayout.MODE_SCROLLABLE);
        mTabs.setupWithViewPager(mVp);
        mTabs.setTabsFromPagerAdapter(adapter);
    }
    class MyAdapter extends FragmentPagerAdapter{
        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return  fragments.get(position);
        }

        @Override
        public int getCount() {
            return titles.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }
    }

    public static String[] getTitles(){

        return new String[]{"全部分类", "演唱会", "音乐会", "话剧歌剧", "舞蹈芭蕾","曲苑杂坛"};

    }
}
